/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef PLJAVA_ATTRIBUTE_CODE_H
#define PLJAVA_ATTRIBUTE_CODE_H

#include <PLJavaFile.h>
#include <filetypes/PLJavaAttribute.h>
#include <filetypes/PLJavaInstructionParser.h>
#include <filetypes/PLJavaAttributeExceptionTable.h>

namespace PLSDK {

/**
 * Code information class of a attribute.
 */
class PLJavaAttributeCode : public PLJavaAttribute, public PLJavaAttributeInterface {
public:
    /**
     * Constructor.
     *
     * @param newMaxStack The max length of the stack.
     * @param newMaxLocals The max locale variables.
     * @param newCodeLength The code length.
     * @param newCode The pointer to the code.
     * @param fileData Pointer to the file data in that the code was executed.
     */
    PLJavaAttributeCode(u2 newMaxStack, u2 newMaxLocals, u4 newCodeLength, uint8_t *newCode, PLJavaFile *fileData);
    /**
     * Get the code parser that parses the java instruction code.
     *
     * @return The parser.
     */
    inline PLJavaInstructionParser *codeParser();
    /**
     * Add new entry to to the exception table.
     *
     * @param newExceptionTable The new entry.
     */
    void addExceptionTable(PLJavaAttributeExceptionTable *newExceptionTable);
    /**
     * Get the exception table entry at index.
     *
     * @return The exception table entry.
     */
    PLJavaAttributeExceptionTable *exceptionAt(u2 index);
    /**
     * Get the count of exception entry's.
     *
     * @return The exception entry.
     */
    inline u2 exceptionTableCount();
    /**
     * Add new attribute information to the attributes.
     *
     * @param newAttribute The attribute to add.
     */
    void addAttribute(PLJavaAttribute *newAttribute);
    /**
     * Get the attribute at a index.
     *
     * @return The attribute.
     */
    PLJavaAttribute *attributeAt(u2 index);
    /**
     * Get the number of attributes.
     *
     * @return The number.
     */
    inline u2 attributesCount();
    /**
     * The max size of the stack that was used for this methode.
     *
     * @return The max size.
     */
    inline u2 maxStack();
    /**
     * The max size that was used for local variables.
     *
     * @return The max size.
     */
    inline u2 maxLocals();
private:
    u2 m_maxStack;
    u2 m_maxLocals;
    PLJavaFile *m_fileData;
    PLJavaInstructionParser m_codeParser;
    PLVector<PLJavaAttributeExceptionTable *> m_exceptionTable;
    PLVector<PLJavaAttribute *> m_attributes;
};

PLJavaInstructionParser *PLJavaAttributeCode::codeParser() {
    return &m_codeParser;
}

u2 PLJavaAttributeCode::exceptionTableCount() {
    return m_exceptionTable.count();
}

u2 PLJavaAttributeCode::attributesCount() {
    return m_attributes.count();
}

u2 PLJavaAttributeCode::maxStack() {
    return m_maxStack;
}

u2 PLJavaAttributeCode::maxLocals() {
    return m_maxLocals;
}

}

#endif
